//
//  FillInformationViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/6.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "FillInformationViewController.h"
#import "FillStoreInfoViewController.h"
#import "MineStoreChooseCityViewController.h"
#import "MineStoreChooseClassController.h"
#import "StorePicViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface FillInformationViewController ()
{
    NSArray *_sectionCityArr;
    NSArray *_cityArr;
    NSArray *_sectionPerArr;
    NSArray *_perArr;
    
    UIDatePicker *_pickerView;
    UIButton *_canceBtn;
    UIView *_BtnbackView;
    
    UIButton *_selectBtn;
    
    NSString *_shopClassId;
    NSString *_cataId;
    NSString *_areaId;
    NSString *_areaIdPath;
    NSString *_areaName;
    
    CGFloat _longitude;
    CGFloat _Latitude;
}
@property (weak, nonatomic) IBOutlet UIButton *startBtn;

@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (weak, nonatomic) IBOutlet UIScrollView *sv;

@property (weak, nonatomic) IBOutlet UILabel *type;
@property (weak, nonatomic) IBOutlet UIButton *cityName;
@property (weak, nonatomic) IBOutlet UITextField *roadName;
@property (weak, nonatomic) IBOutlet UITextField *address;
@property (weak, nonatomic) IBOutlet UITextField *otherShop;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UILabel *per;
@property (weak, nonatomic) IBOutlet UIButton *cliclBtn;

@end

@implementation FillInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"填写基础资料";
    
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    if (IPHONE5S) {
        _sv.contentSize = CGSizeMake(0, HEIGHT);
    }
    _sectionCityArr = [NSArray array];
    _cityArr = [NSArray array];
    _sectionPerArr = [NSArray array];
    _perArr = [NSArray array];
    
    [self createPickerView];
    [self downloadData];
}
- (IBAction)chooseArea:(UIButton *)sender {
    MineStoreChooseCityViewController *chooseCity = [[MineStoreChooseCityViewController alloc]init];
    chooseCity.data = _sectionCityArr;
    chooseCity.backType = ^(NSString *area,NSString*areaIdPath,NSString*areaId) {
        _areaIdPath = areaIdPath;
        _areaName = area;
        _areaId = areaId;
        
        [sender setTitle:area forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:chooseCity animated:YES];
}
- (IBAction)chooseType:(UIButton*)sender {
    MineStoreChooseClassController *chooseClass = [[MineStoreChooseClassController alloc]init];
    chooseClass.data = _sectionPerArr;
    chooseClass.backType = ^(NSString *type,NSString *catId,NSString *classid,NSString *per) {
        _cataId = [NSString stringWithFormat:@"%@",catId];
        _shopClassId = [NSString stringWithFormat:@"%@",classid];
        _per.text = [NSString stringWithFormat:@"%@",per];;
        [sender setTitle:type forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:chooseClass animated:YES];
}
- (IBAction)chooseTime:(UIButton *)sender {
    _selectBtn = sender;
    _pickerView.hidden = NO;
    _BtnbackView.hidden = NO;
}
- (void)createPickerView
{
    _BtnbackView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-230, WIDTH, 30)];
    _BtnbackView.backgroundColor = [UIColor colorWithHex:0x999999];
    _BtnbackView.hidden = YES;
    [self.view addSubview:_BtnbackView];
    
    _pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, HEIGHT-200, WIDTH, 200)];
    _pickerView.datePickerMode = UIDatePickerModeTime;
    _pickerView.backgroundColor = [UIColor colorWithHex:0x999999];

    _pickerView.hidden = YES;
    [self.view addSubview:_pickerView];
    
    _canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _canceBtn.frame = CGRectMake(10, 0, 50, 30);
    [_canceBtn setTitle:@"确定" forState:UIControlStateNormal];
    _canceBtn.hidden = NO;
    [_canceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_canceBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [_BtnbackView addSubview:_canceBtn];
}
- (void)btnClick
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //设定转换模式
    formatter.dateFormat = @"HH:mm:ss";
    
    NSString *stringFromDate = [formatter stringFromDate:_pickerView.date];
    [_selectBtn setTitle:stringFromDate  forState:UIControlStateNormal];
    [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    NSDate *start =[self isDate:_startBtn.currentTitle];
    NSDate *end = [self isDate:_endBtn.currentTitle];
    if (start&&end) {
        if ([self compareDate:start withDate:end]==1) {
            _pickerView.hidden = YES;
            _BtnbackView.hidden = YES;
            _selectBtn = nil;
        }else{
            [self.view Message:@"结束时间必须大于开始时间"];
        }
    }else{
        _pickerView.hidden = YES;
        _BtnbackView.hidden = YES;
        _selectBtn = nil;
    }
    
}
-(NSDate*)isDate:(NSString*)dateStr
{
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"HH:mm:ss"];
    NSDate *dta = [[NSDate alloc] init];
    
    dta = [dateformater dateFromString:dateStr];
    return dta;
}
- (NSInteger)compareDate:(NSDate*)dta withDate:(NSDate*)dtb
{
    int aa =0;
    NSComparisonResult result = [dta compare:dtb];
    if (result==NSOrderedSame)
    {
        //        相等  aa=0
    }else if (result==NSOrderedAscending)
    {
        //bDate比aDate大
        aa=1;
    }else if (result==NSOrderedDescending)
    {
        //bDate比aDate小
        aa=-1;
    }
    return aa;
}
- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineStoreFill) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
    
        _sectionCityArr = responseObject[@"data"][@"city"];
        _sectionPerArr =responseObject[@"data"][@"class"];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (IBAction)JoinClick:(UIButton *)sender {
    
    if (!_phone.text.length||!_shopClassId||!_cataId||!_areaId||!_address.text.length||!_roadName.text.length||!_startBtn.currentTitle.length||!_endBtn.currentTitle.length||!_areaIdPath||!_otherShop.text.length) {
        [self.view Message:@"请填写信息"];
        return;
    }
    NSString *str = [NSString stringWithFormat:@"%@%@%@",_areaName,_roadName.text,_address.text];
    [self.view loadingOnAnyView];
    _cliclBtn.enabled = NO;
    [self getLongitudeAndLatitude:str];
}
- (void)next
{
    [self.view removeAnyView];
    _cliclBtn.enabled = YES;
    NSString *str = [NSString stringWithFormat:@"%@%@%@",_areaName,_roadName.text,_address.text];
    NSDictionary *para = @{@"shopName":_otherShop.text,@"isSelf":@"0",@"shopType":_shopType,@"shopName":_otherShop.text,@"telephone":_phone.text,@"shop_class":_shopClassId,@"cata_id":_cataId,@"areaId":_areaId,@"areaIdPath":_areaIdPath,@"shopAddress":str,@"shopAtive":@"1",@"serviceStartTime":_startBtn.currentTitle,@"serviceEndTime":_endBtn.currentTitle,@"shopStatus":@"1",@"userToken":[Utils getUserToken],@"jwd":[NSString stringWithFormat:@"%f|%f",_longitude,_Latitude],@"statusDesc":@"正常营业",};
    
    StorePicViewController *pic = [[StorePicViewController alloc] init];
    pic.para = para;
    [self.navigationController pushViewController:pic animated:YES];
}
- (void)getLongitudeAndLatitude:(NSString*)str
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
        if ([placemarks count] > 0 && error == nil) {
            NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
            NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
            
            _Latitude =firstPlacemark.location.coordinate.latitude;
            _longitude =firstPlacemark.location.coordinate.longitude;
            [self next];
            
        }
        else if ([placemarks count] == 0 && error == nil) {
            NSLog(@"Found no placemarks.");
            [self.view Message:@"地址找不到"];
        } else if (error != nil) {
            NSLog(@"An error occurred = %@", error);
            [self.view Message:@"地址找不到"];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
